<template>
	<view>
		<view class="content">
			<u-cell title="议题模板" :isLink="true" :value="content_type" style='border:0' @click="opentopic_template">
			</u-cell>
			<view class="">
				<u--textarea v-model="content" placeholder="请输入议题内容" height="150" confirmType="done"></u--textarea>
			</view>
			<!-- <view class="img-box">
				<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="8" width="105" height="105" uploadText="添加图片" uploadIcon="plus" :previewFullImage="true">
				</u-upload>
			</view> -->
			<view class="file-box content-box ">
				<u-upload :fileList="fileList1" :previewImage="false" @afterRead="afterRead1" @delete="deletePic1"
									accept="file" :maxCount="100" name="1" multiple uploadText="上传附件" uploadIcon="plus">
									<view class="content-box">
										<div style="position: relative" v-if="show_files" v-for="(item,index) in files">
											<div class="file-name" @click.stop="handleDownload(item)">
												<u-icon name="attach" color="#4871C0" size="24"></u-icon>
												<label class="file-txt">{{item.filename}}</label>
											</div>
											<view class="new-tip" @click.stop="deletePic1(item)">
												<u--image :src="delete_icon" width="40rpx" height="28rpx">
												</u--image>
											</view>
										</div>
										<view class="file-tip" @click="afterRead1">
											上传附件
										</view>
									</view>
								</u-upload>
			</view>
			<view class="tishititle">仅支持上传word、excel、ppt、pdf、图片类型的文件</view>
			<view class="button" @click="isLoading?'':save()">
				{{isLoading?'正在提交':'提交审核'}}
			</view>
		</view>
		<u-popup :show="showTemp" mode="bottom" @close="closetopic_template" @open="opentopic_template"
			:closeable="true" :border-radius="10" close-icon-color="#010101" :mask-close-able="false"
			close-icon-size="42">
			<view class="content" style="height:60%">
				<view class="title">请选择议题模板</view>
				<view @click="seltopic_template(v)" :class="select==v.id?'meet active':'meet '"
					v-for="(v, i) in topic_template">
					{{ v.title }}
					<text>
						<img src="@/static/assets/images/dui.png" v-if="select==v.id" /></text>
				</view>

				<view class="btn">
					<u-button class='cancelbtn' @click="closetopic_template">取消</u-button>
					<u-button type="primary" text="确定" @click="suretopic_template" color='#4871C0'></u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		HTTP_REQUEST_URL,
	} from '@/config/app';
	import {
		addTopicEdit,
		editdetail,
		meet_topic_template
	} from "@/api/meetApi";
	export default {
		data() {
			return {
				delete_icon: require('@/static/delete.png'),
				id: '',
				content: '',
				content_type: '',
				fileList: [],
				fileList1: [],
				fileList2:[],
				images: [],
				files: [],
				fileName: null,
				show_files: false,
				form: {},
				showTemp: false,
				topic_template: [],
				select: 0,
				select_topic: {},
				isLoading:false
			}
		},
		onLoad(options) {
			this.getmeet_topic_template();
			this.id = options.id;
			this.getdataDeatil()
			console.log('--------this.$httpApi', this.$httpApi)
		},
		methods: {
			// 获取议题模板
			getmeet_topic_template() {
				meet_topic_template().then(res => {
					console.log(res)
					if (res.code == 1) {
						this.topic_template = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						});
					}

				})
			},
			opentopic_template() {
				this.showTemp = true;
			},
			closetopic_template() {
				this.showTemp = false;
			},
			suretopic_template() {
				this.showTemp = false;
				this.content = this.select_topic.content;
				this.content_type = this.select_topic.title;
			},

			seltopic_template(item) {
				this.select_topic = item
				this.select = item.id;
			},
			// 点击附件预览
			handleDownload(item) {
				console.log("=====item", item)
				let params = {
					url: item.fullurl, // 需要预览文件的地址(必填，可以使用相对路径)
					name: item.filename, // 需要预览文件的文件名(不填的话取url的最后部分)
					size: item.filesize, // 需要预览文件的字节大小(必填)
					type: item.mimetype
				}
				this.$store.dispatch('yulan', params)
			},
			// 保存
			save() {
				if (!this.isLoading) {
					if (!this.content) {
						uni.showToast({
							title: '请填写议题内容',
							icon: 'none',
							duration: 1000
						});
						return;
					}
					if (this.files.length < 1) {
						uni.showToast({
							title: '请上传附件',
							icon: 'none',
							duration: 1000
						});
						return;
					}
				
					let params = {
						id: this.id,
						content: this.content,
						files: JSON.stringify(this.files || [])
					};
					this.isLoading = true;
					//显示加载框
					uni.showLoading({
						title: '正在提交'
					})
				addTopicEdit(params).then(res => {
					console.log(res)
					if (res.code == 1) {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							});
							this.isLoading = false;
							uni.hideLoading();
						}, 2000)
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						});
						this.isLoading = false;
						uni.hideLoading();
					}
					
				})
				}
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
				this.images.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log("result：", result)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					this.images.push(result)
					console.log("images:", this.images)
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: HTTP_REQUEST_URL + '/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log("res:", res)
							if (JSON.parse(res.data).code == 1) {
								setTimeout(() => {
									resolve(JSON.parse(res.data).data.files)
								}, 500)
							} else {
								uni.showToast({
									title: JSON.parse(res.data).msg,
									icon: 'none',
									duration: 1000
								});
							}

						}
					});
				})
			},
			// 新增文件
			async afterRead1(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [];
				if(event.file){lists=event.file}
				let fileListLen = this.fileList1.length
				lists.forEach((item) => {
					this.fileList1.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise1(lists[i].url)
					let item = this.fileList1[fileListLen]
					this.fileList1.push({
						...item,
						status: 'success',
						message: '',
					})
					this.show_files=true
					this.files.push(result)
					fileListLen++
				}
			},

			uploadFilePromise1(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$httpApi + '/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'hy'
						},
						success: (res) => {
							console.log("res:", res)
							if (JSON.parse(res.data).code == 1) {
								setTimeout(() => {
									resolve(JSON.parse(res.data).data.files)
								}, 500)
							} else {
								uni.showToast({
									title: JSON.parse(res.data).msg,
									icon: 'none',
									duration: 1000
								});
							}
						}
					});
				})
			},
			deletePic1(event) {
				let _this=this
				uni.showModal({
					title: '提示',
					content: '您确定要删除吗?',
					success: function(res) {
						if (res.confirm) {
							_this.fileList1.splice(event.index, 1)
							_this.files.splice(event.index, 1)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},
			getdataDeatil() {
				let params = {
					id: this.id
				};
				editdetail(params).then(res => {
					if (res.code = 1) {
						this.content = res.data.content;
						if (res.data.images) {
							this.images = JSON.parse(JSON.stringify(res.data.images))
							console.log("============ this.images:", this.images)
							this.fileList = JSON.parse(JSON.stringify(res.data.images));
						}
						this.show_files = true
						if (res.data.files.length>0) {
							// this.fileList2 = res.data.files;
							// this.files = res.data.files;
							this.files = JSON.parse(JSON.stringify(res.data.files))
							console.log("============ this.files:", this.files)
							this.fileList1 = JSON.parse(JSON.stringify(res.data.files));
						}
						// this.list2 = res.data.data;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						});
					}
				})
			}
		}
	}
</script>

<style>
	@import "@/static/assets/css/index.css";
	@import "@/static/assets/css/mobiscroll.core-2.5.2.css";
	@import "@/static/assets/css/mobiscroll.animation-2.5.2.css";
	@import "@/static/assets/css/mobiscroll.android-ics-2.5.2.css";

	.tishititle {
		font-size: 20rpx;
		color: #ccc;
		padding-top: 20rpx;
	}

	.content {
		background-color: #F8F8F8;
		height: 100vh;
		padding: 30rpx;
	}

	.img-box {
		margin: 30rpx 0;
	}

	/deep/.u-upload__button {
		background-color: #fff;
	}

	.file-box {
		width: 100%;
		margin-top: 20rpx;
	}

	.file-tip {
		background-color: #fff;
		padding: 30rpx;
		width: 620rpx;
		font-size: 28rpx;
		font-weight: 500;
		color: #999999;
	}

	.button {
		background: #4871C0;
		border-radius: 8rpx;
		opacity: 1;
		font-size: 28rpx;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
		padding: 30rpx 0;
		position: fixed;
		width: calc(100% - 60rpx);
		bottom: 10rpx;
		left: 30rpx;
	}

	/deep/.file-box .u-upload__wrap__preview {
		width: 670rpx !important;
		/* height: 88rpx!important; */
	}

	/deep/.file-box .u-upload__button .u-icon {
		display: none !important;
	}

	/deep/.file-box .u-upload__wrap__preview .u-icon {
		display: none !important;
	}

	.file-name {
		padding: 10px;
		background: #F8F9FC;
		border-radius: 8px 8px 8px 8px;
		position: relative;
	}
.file-txt{
	word-break: break-all;
}
	.new-tip {
		position: absolute;
		right: 0px;
		top: 0;
		/* width: 30px;
		text-align: center;
		height: 16px;
		background: #FF7A00;
		border-radius: 0px 8px 0px 4px;
		font-size: 8px;
		color: #ffffff; */
	}

	.pic_user {
		width: 38px;
		height: 38px;
		margin-bottom: 20px;
		background: #F0F0F0;
		border-radius: 4px 4px 4px 4px;
		background: #F0F0F0 url('@/static/assets/images/pick_user.png') no-repeat center center;
		background-size: 40% 40%;
	}

	.u-flex {
		display: flex;
		align-items: center;
	}

	.file-name {
		padding: 10px;
		background: #fff;
		border-radius: 8px 8px 8px 8px;
		display: flex;
		flex-direction: row;
		position: relative;
		width: calc(100% - 20px);
		font-size: 30rpx;
		color: #4871C0;
		/* margin-left: 3.5vw; */

	}

	.content-box {
		width: calc(100vw - 60rpx);
		min-height: 92rpx;
		background-color: #fff;
	}

	.file-tip {
		background-color: #fff;
		padding: 30rpx;
		width: calc(100vw - 48pt);
		font-size: 28rpx;
		font-weight: 500;
		color: #999999;
	}
</style>
